package com.laboratory.serviceprovide.workBench.service.impl;

import com.Laboratory.workBench.pojo.booking;
import com.Laboratory.workBench.pojo.room;
import com.Laboratory.workBench.service.roomService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.laboratory.serviceprovide.constant.constantArgs;
import com.laboratory.serviceprovide.workBench.mapper.roomMapper;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @Author acok
 * @description:TODO
 * @Date 2022/12/16 14:44
 * @Version 1.0
 */
@DubboService(interfaceClass = roomService.class,version = "1.0.0")
public class roomServiceImpl implements roomService {
    @Resource
    private roomMapper roomMapper;
    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public List<room> selectRoomListByTime(String applyDate,String applyTime) {
        return roomMapper.selectRoomListByTime(applyDate, applyTime);
    }

    @Override
    public int insertBookingRoomRecord(booking booking) {
        return roomMapper.insertBookingRoomRecord(booking);
    }

    @Override
    public List<Map<Object, Object>> getRoomMsgList() {
        List<Map<Object,Object>> roomNoList=null;
        ValueOperations<String, String> ops = stringRedisTemplate.opsForValue();
        if (ops.get(constantArgs.REDIS_ROOM_NO_KEY)==null){
            roomNoList=roomMapper.getRoomMsgList();
            if(roomNoList.size()>0) {
                ops.set(constantArgs.REDIS_ROOM_NO_KEY, JSON.toJSONString(roomNoList), 30 * 60, TimeUnit.SECONDS);
            }
        }else{
            roomNoList=JSON.parseObject(ops.get(constantArgs.REDIS_ROOM_NO_KEY),new TypeReference<List<Map<Object,Object>>>(){});
        }
        return roomNoList;
    }

    @Override
    public List<booking> getRoomBookingList(Integer pageNo,Integer pageSize) {
        return roomMapper.getRoomBookingList(pageNo, pageSize);
    }
}
